package com.mmp.main.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mmp.common.servlet.BasicServlet;
import com.mmp.main.entity.Hospital;
import com.mmp.main.entity.Site;
import com.mmp.main.util.WebUtil;

public class ChooseSessionHospitalProcessServlet extends BasicServlet {

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {

		if (!WebUtil.userHasRightToChangeSessionHospital(request)) {
			response.sendError(HttpServletResponse.SC_FORBIDDEN);
			return;
		}

		int siteId = WebUtil.getParamValueAsInteger(request, "site", 0);
		int locationId = WebUtil.getParamValueAsInteger(request, "hospital", 0);

		getSessionUser(request).getEmployee().setSite(dbQueryManager.findById(siteId, Site.class));

		if (locationId != 0)
			setSessionLocation(request, dbQueryManager.findById(locationId, Hospital.class));
		else
			setSessionLocation(request, null);

		removeSessionAttribute(request, "purpose");
		sendRedirect(request, response, "UserProfileDisplay");
	}

}
